var startPos = {x:0,y:0}
var engPos = {x:0,y:0}
var centerPos = {x:0,y:0}
var _src = '';
var offsetTop = 0;
var offsetLeft = 0;
var imgWidth = 0;
var imgHeight = 0;
var scrollFunc ;
var size = 1;
function maxImg(_this) {
    $('body').css('overflow-y','hidden');
	_src = $(_this).attr("src")
	var html =  '<div class="maxImg-box">'+
				'<div class="maxImgCover" onclick="maxImgClose()"></div>'+
				'<div class="maxImg-div">'+
				'<img class="maxImg" src='+ _src +' />'+
				'<div class="maxImgTouch"></div>'+
				'</div>'+
				'</div>';
	$(html).appendTo("body");
	imgWidth = $(".maxImg").width();
	imgHeight = $(".maxImg").height();
	$(".maxImg-div").css({"top":($(window).height() - imgHeight)/2,"left":($(window).width() - imgWidth)/2})
	offsetTop = $(".maxImg-div")[0].offsetTop;
	offsetLeft = $(".maxImg-div")[0].offsetLeft;
	maxImgTouchstart();
	windowAddMouseWheel();
	getCenter();


};
/*拖动大图*/
function maxImgTouchstart(){
	$(".maxImg-box").off();
	$(".maxImgTouch").on("mousedown",function(e){
		startPos = {x:e.pageX,y:e.pageY};
		offsetTop = $(".maxImg-div")[0].offsetTop;
		offsetLeft = $(".maxImg-div")[0].offsetLeft;
		maxImgTouchmove();
		maxImgTouchup();
	})
}
function maxImgTouchmove(){
	$(".maxImg-box").off();
	$(".maxImgTouch").on("mousemove",function(e){
		engPos = {x:e.pageX,y:e.pageY};
		var top = offsetTop + engPos.y - startPos.y
		var left = offsetLeft + engPos.x - startPos.x
		/*if(top < 0){
			top = 0;
		}
		if(left < 0){
			left = 0
		}*/
		/*if(top > $(window).height() - imgHeight){
			top = $(window).height() - imgHeight
		}
		if(left > $(window).width() - imgWidth){
			left = $(window).width() - imgWidth
		}*/
		$(".maxImg-div").css({"top":top,"left":left})
	})
}
/*拖动结束*/
function maxImgTouchup(){
	$(".maxImgTouch").on("mouseup mouseleave",function(e){
		$(".maxImgTouch").off()
		maxImgTouchstart();
	})
}
/*关闭大图*/
function maxImgClose(){
    $('body').css('overflow-y','auto');
	$(".maxImg-box").on("click",function(){
		$(".maxImg-box").fadeOut();
		setTimeout(function(){
			$(".maxImg-box").remove();
		},300)
		size = 1
	})
    window.onmousewheel = document.onmousewheel = null;  
}
/*注册滚轮事件*/
function windowAddMouseWheel() {  
    scrollFunc = function (e) {  
        e = e || window.event;  
        if (e.wheelDelta) {  //判断浏览器IE，谷歌滑轮事件  
            if (e.wheelDelta > 0) { //当滑轮向上滚动时
            	size = (size + 0.01)
            }  
            if (e.wheelDelta < 0) { //当滑轮向下滚动时  
            	size = (size - 0.01)
            }  
        } else if (e.detail) {  //Firefox滑轮事件  
            if (e.detail> 0) { //当滑轮向上滚动时  
            	size = (size + 0.01)
            }  
            if (e.detail< 0) { //当滑轮向下滚动时  
            	size = (size - 0.01)
            }
        }
        if(size < 0){
        	return
        }
        var test = size.toFixed(2);
        $(".maxImg-div").css({
			'transform': "scale(" + size + "," + size +")",
			'-ms-transform': "scale(" + size + "," + size +")",
			'-webkit-transform': "scale(" + size + "," + size +")",
			'-moz-transform': "scale(" + size + "," + size +")",
			'-o-transform': "scale(" + size + "," + size +")",
		})
    };  
}
/*放大获取图片中心点*/
function getCenter(e){
	$(".maxImg-div").on("mousemove",function(e){
		/*centerPos = {x:e.pageX - offsetLeft ,y:e.pageY - offsetTop};
		$(".maxImg-div").css({
			'transform-origin': centerPos.x + "px " + centerPos.y + "px",
			'-ms-transform-origin': centerPos.x + "px " + centerPos.y + "px",
			'-webkit-transform-origin': centerPos.x + "px " + centerPos.y + "px",
			'-moz-transform-origin': centerPos.x + "px " + centerPos.y + "px",
			'-o-transform-origin': centerPos.x + "px " + centerPos.y + "px",
		})*/
    	window.onmousewheel = document.onmousewheel = scrollFunc;  
	})
	$(".maxImg-div").on("mouseout",function(e){
    	window.onmousewheel = document.onmousewheel = null;  
	})
}
